
/**
 *设置表单效果
 */

;function setFrom(form,opts){
	var form=$(form),
		action=form.attr('action');
	if(!(form.length&&action))return;
	
	$('<input type="submit" style="display:none;" /><input type="reset" style="display:none;"/>').appendTo(form);
	
	var options=$.extend(true,{
			validationEngine:{
				showDuration:200,
				addFailureCssClassToField:'error',
				addSuccessCssClassToField:'success'
			},
			onError:'',
			onCancel:$.noop,
			onSuccess:function(){
				Tips('success');
			}
		},opts);
	form.validationEngine(
			options.validationEngine
		)
		.delegate('a[role]',click,function(){
			switch($(this).attr('role')){
				case 'submitBtn':
					form.submit();	
				break;
				case 'cancelBtn':
					form.find('input[type="reset"]').trigger(click);		
						form.validationEngine("hideAll").find(':input').removeClass('error success');
						options.onCancel();
				break;
			}
		})
		.submit(function(){
			if(form.validationEngine("validate")){
				 rpcJSON(action,getFormValue(form),function(backData){
					options.onSuccess(backData);									  
				},options.onError);
			};
			return false;
		}).find('input').attr('autocomplete','off');
		
};

;function resetImageHtml(html){
	var newHtml=$('<div>'+html+'</div>');
	newHtml.find('img').each(function(){
		var $this=$(this);
		$this.removeAttr('width').removeAttr('height');
		if(!$this.hasClass('responsiveImage')){
			$this.addClass('responsiveImage');
		};
	});
	return newHtml.html();
};

/**
 *设置编辑器
 */

;function textareaToEditor(el,opts){
	var editor,
		opt={
				items : [
						'fontname', 'fontsize', 'formatblock','|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
						'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
						'insertunorderedlist', '|', 'image', 'insertfile', 'link', 'unlink','table','hr'],
				 fontSizeTable : ['9px', '10px', '11px', '12px', '14px', '16px', '18px', '24px', '32px', '48px', '72px'],
				 resizeType : 2,
				 width:600,
				 minWidth:450,
				 pasteType:1,
				 themeType : 'simple',
				 uploadJson:kindEditor_upload,
				 afterCreate:function(){
					 if(editor){
						$(el).val(resetImageHtml(editor.html()).replace(/\'/g,"&#8217;"));
					 };
				 },
				 afterBlur:function(){
					 if(editor){
						 $(el).val(resetImageHtml(editor.html()).replace(/\'/g,"&#8217;"));
					 };
				 }
		 };
	if(typeof xheditorUpImgUrl=='string'){
		opt.upImgUrl=xheditorUpImgUrl;
		opt.upImgExt="jpg,jpeg,gif,png";
	};

	var options=$.extend(true,opt,opts);

	editor=KindEditor.create(el, options);
	
	return editor;
};

/**
 *ajax图标数字分页
 */
 
;function iconPage(showPage,nowPage,totalPage,container,fn){
			if(totalPage<2){
				return;
			}else{
				if(nowPage>1){
					pages='<a data-page="'+(nowPage-1)+'" href="javascript:void(0);">&lt;</a>';
				}else{
					pages='';
				};
				if(nowPage<=Math.floor(showPage/2)){
					var endPage=showPage<=totalPage?showPage:totalPage;
					for(i=1;i<=endPage;i++){
						pages+='<a data-page="'+i+'" href="javascript:void(0);">'+i+'</a>';
					};
				}else{
					var startPage=nowPage-Math.floor(showPage/2);
					
					if(totalPage>startPage+showPage){
						for(i=startPage;i<startPage+showPage;i++){
							pages+='<a data-page="'+i+'" href="javascript:void(0);">'+i+'</a>';
						}
					}else{
						startPage=showPage>=totalPage?1:totalPage-showPage+1;
						for(i=startPage;i<=totalPage;i++){
							pages+='<a data-page="'+i+'" href="javascript:void(0);">'+i+'</a>';
						}
					}
				};
				if(nowPage<totalPage){
					pages+='<a data-page="'+(nowPage+1)+'" href="javascript:void(0);">&gt;</a>';
				};
				container.html(pages).find('a').click(function(){
					fn($(this).data('page'));														   
				});
				container.find('a[data-page="'+nowPage+'"]').addClass('active');
			};
};

/**
 *ajax上下分页
 */
 
;function textPage(showPage,nowPage,totalPage,container,fn){
			if(totalPage<2){
				return;
			}else{
				if(nowPage>1){
					pages='<a data-page="'+(nowPage-1)+'" href="javascript:void(0);">上一页</a>';
				}else{
					pages='';
				};
				if(showPage){
					pages+='&nbsp;&nbsp;'+nowPage+'/'+totalPage+'&nbsp;&nbsp;';
				};
				if(nowPage<totalPage){
					pages+='<a data-page="'+(nowPage+1)+'" href="javascript:void(0);">下一页</a>';
				};
				container.html(pages).find('a').click(function(){
					fn($(this).data('page'));														   
				});
			};
};

/**
 *本页面打开窗口
 */
 
;function openWindowLocation(src,callBack){
	if(typeof top.openIframeWindow=='function'){
		top.openIframeWindow(src,callBack);
	}else{
		window.location.href=src;
	}
};


/**
 *树菜单窗口
 */
 
;function treeWindow(opts){
	var options=$.extend(true,{
						 	title:'选择分类',
							path:'',
							url:ajaxURL,
							data:{},
							tree:{
								checkbox:false,
								selectMode: 3
							},
							win:{
								onBeforeClose:function(fn,type){
									if(type=='confirm'&&!dialog.selectedData){
										top.Alert('还没有任何选择');
									}else{
										fn();
									}
								}
							}
						 },opts),
		el=$('<div style="margin-bottom:10px; padding-bottom:10px; border-bottom:1px solid #e6e6e6;">'+options.title+'：<span role="title">'+options.path+'</span></div><div role="tree"></div>'),
		dialog=top.ConfirmDialog(options.title,el,options.win),
		titleEl=dialog.find('[role=title]');
	dialog.selectedData='';
	dialog.find('[role=tree]').dynatree($.extend(true,{
		autoFocus: false,
		initAjax: {
			url: options.url,
			data:options.data
		},
		onActivate: function(node) {
			titleEl.html(node.data.path);
			dialog.selectedData=node.data;
		},
		onLazyRead: function(node){
			var nodeData=options.data;
			nodeData.parentId=node.data.id;
			node.appendAjax({
				url: options.url,
				data: nodeData,
				success:function(){
					dialog.find('div.boxscroll:first').getNiceScroll().resize();
				}
			});
		},
		onExpand: function() {
			dialog.find('div.boxscroll:first').getNiceScroll().resize();
		}
	},options.tree));
	dialog.find('div.boxscroll:first').getNiceScroll().resize();
	return dialog;
};

/**
 *选择产品窗口
 */

;function selectProduct(opts){
	var options=$.extend(true,{
			title:'选择产品',
			multi_selection:false,
			selectedData:[],
			url:'',
			data:{
				
			},
			win:{
				onBeforeClose:function(fn,type){
					if(type=='confirm'&&!dialog.selectedData.length){
						top.Alert('还没有任何选择');
					}else{
						fn();
					}
				}
			}
		},opts);
	var el=$('<div><div><a class="b_4 miniBtn" href="javascript:;" role="selectBtn" style="color:#fff;">选择分类</a>&nbsp;&nbsp;&nbsp;<span role="others"><input type="checkbox" name="recommend" />推荐&nbsp;&nbsp;<input type="checkbox" name="sale" />特价&nbsp;&nbsp;<input type="checkbox" name="news" />新品&nbsp;&nbsp;<input type="checkbox" name="hot" />热门</span>&nbsp;&nbsp;&nbsp;<span><input name="keyWord" class="input-mini" type="text"  style="width:250px;" placeholder="搜索产品" /></span>&nbsp;<a class="b_1 miniBtn" href="javascript:;" role="searchBtn">搜索产品</a>&nbsp;<a class="b_1 miniBtn" href="javascript:;" role="allBtn" style="display:none;">全部产品</a></div><div class="pt10">产品分类：<span role="mainTitle">全部</span></div><div role="container">'+getContentLoading()+'</div></div>'),
		dialog=top.ConfirmDialog(options.title,el,options.win);
	
	var page=1,
		size=10,
		keyWord='',
		categoryId=0,
		getList=function(){
			var data=$.extend(true,options.data,{
					size:size,
					page:page,
					categoryId:categoryId,
					keyWord:keyWord,
					recommend:$('input[name="recommend"]',el).attr('checked')?1:0,
					sale:$('input[name="sale"]',el).attr('checked')?1:0,
					news:$('input[name="news"]',el).attr('checked')?1:0,
					hot:$('input[name="hot"]',el).attr('checked')?1:0
				});
			el.find('div[role=container]')
			.html(getContentLoading())
			.load(options.url,data
			,function(){
				$.each(dialog.selectedData,function(i,item){
					el.find('div[role=container] li[data-id='+item.id+']').addClass('active');								
				});
				el.find('div[role=container] li').each(function(){
					$(this).attr({




								 'title':$(this).find('p').html(),
								 'data-tips':'3_t'





								 })											
				});
			});
		};
	
	$(':checkbox',el).on('change',function(){
		if($(this).attr('checked')){
			$('a[role="allBtn"]',el).show();
		};
		getList();								   
	});
	
	el.delegate('.selectPage a',click,function(e){
		e.preventDefault();
		e.stopPropagation();
		if(page!=$(this).data('id')){
			page=$(this).data('id');
			getList();
		}
	}).delegate('a[role]',click,function(e){
		switch($(this).attr('role')){
			case 'searchBtn' :
				var keyWordEl=el.find('input[name=keyWord]');
				if(!keyWordEl.val()){
					top.Tips('error','关键字不能为空');
					return;
				};
				if(keyWord!=keyWordEl.val()){
					keyWord=keyWordEl.val();
					page=1;
					getList();
					el.find('a[role=allBtn]').show();
				};
				break;
			
			case 'allBtn':
				var keyWordEl=el.find('input[name=keyWord]');
				keyWord='';
				keyWordEl.val('');
				categoryId=0;
				$('span[role="mainTitle"]',el).html('全部');
				$('span[role="others"] :checkbox',el).attr('checked',false);
				page=1;
				getList();
				$(this).hide();
				break;
			
			case 'selectBtn':
				var sWindow=treeWindow({
					path:$('span[role=mainTitle]',el).html(),
					url:ajaxURL,
					data:{
						action:'getCategory',
						parentId:'-1'
					},
					win:{
						onConfirm:function(){
							categoryId=sWindow.selectedData.id;
							page=1;
							keyWord='';
							$('input[name=keyWord]',el).val('');
							$('span[role=mainTitle]',el).html(sWindow.selectedData.path);
							getList();
							$('a[role=allBtn]',el).show();
						}
					}
				});
				break;
		}
	}).delegate('div.w_brandList li',click,function(){
		var $this=$(this);
		if(options.multi_selection){
			if($this.hasClass('active')){
				$this.removeClass('active');
				dialog.selectedData=removeArray(dialog.selectedData,{
					id:$this.data('id'),
					name:$this.find('p').html()
				});
				   
			}else{
				$this.addClass('active');
				dialog.selectedData.push({
					id:$this.data('id'),
					name:$this.find('p').html()
				});
			}
		}else{
			$this.toActive();
			dialog.selectedData=[{
				id:$this.data('id'),
				name:$this.find('p').html()
			}];
		}
	});
	
	getList();

	dialog.selectedData=options.selectedData;
	
	return dialog;
};

/**
 *选择品牌窗口
 */

;function selectBrand(opts){
	var options=$.extend(true,{
			title:'选择品牌',
			multi_selection:false,
			selectedData:[],
			url:'',
			data:{
				
			},
			win:{
				onBeforeClose:function(fn,type){
					if(type=='confirm'&&!dialog.selectedData.length){
						top.Alert('还没有任何选择');
					}else{
						fn();
					}
				}
			}
		},opts);
	var el=$('<div><div><span><input name="keyWord"  class=""inputText small" type="text"  style="width:250px;" placeholder="搜索品牌" /></span>&nbsp;<a class="b_1 miniBtn" href="javascript:;" role="searchBtn">搜索品牌</a>&nbsp;<a class="b_1 miniBtn" href="javascript:;" role="allBtn" style="display:none;">全部品牌</a></div><div role="container">'+getContentLoading()+'</div></div>'),
		dialog=top.ConfirmDialog(options.title,el,options.win);
	
	var page=1,
		size=10,
		keyWord='',
		getList=function(){
			var data=$.extend(true,options.data,{page:page,size:size,keyWord:keyWord});
			el.find('div[role=container]')
			.html(getContentLoading())
			.load(options.url,data
			,function(){
				$.each(dialog.selectedData,function(i,item){
					el.find('div[role=container] li[data-id='+item.id+']').addClass('active');								
				});
				el.find('div[role=container] li').each(function(){
					$(this).attr({
								 'title':$(this).find('p').html(),
								 'data-tips':'3_t'
								 })											
				});
				
			});
		};
		
	el.delegate('.selectPage a',click,function(e){
		e.preventDefault();
		e.stopPropagation();
		if(page!=$(this).data('id')){
			page=$(this).data('id');
			getList();
		}
	}).delegate('a[role]',click,function(e){
		switch($(this).attr('role')){
			case 'searchBtn' :
				var keyWordEl=el.find('input[name=keyWord]');
				if(!keyWordEl.val()){
					top.Tips('error','关键字不能为空');
					return;
				};
				if(keyWord!=keyWordEl.val()){
					keyWord=keyWordEl.val();
					page=1;
					getList();
					el.find('a[role=allBtn]').show();
				};
				break;
			
			case 'allBtn':
				var keyWordEl=el.find('input[name=keyWord]');
				keyWord='';
				keyWordEl.val('');
				page=1;
				getList();
				$(this).hide();
				break;
		}
	}).delegate('div.w_brandList li',click,function(){
		var $this=$(this);
		if(options.multi_selection){
			if($this.hasClass('active')){
				$this.removeClass('active');
				dialog.selectedData=removeArray(dialog.selectedData,{
					id:$this.data('id'),
					name:$this.find('p').html()
				});
				   
			}else{
				$this.addClass('active');
				dialog.selectedData.push({
					id:$this.data('id'),
					name:$this.find('p').html()
				});
			}
		}else{
			$this.toActive();
			dialog.selectedData=[{
				id:$this.data('id'),
				name:$this.find('p').html()
			}];
		}
	});
	
	getList();
	
	dialog.selectedData=options.selectedData;
	
	return dialog;
};

/**
 *选择内容窗口
 */

;function selectArticle(opts){
	var options=$.extend(true,{
			title:'选择内容',
			multi_selection:false,
			selectedData:[],
			url:'',
			data:{
				
			},
			win:{
				onBeforeClose:function(fn,type){
					if(type=='confirm'&&!dialog.selectedData.length){
						top.Alert('还没有任何选择');
					}else{
						fn();
					}
				}
			}
		},opts);
	var el=$('<div><div><a class="b_4 miniBtn" href="javascript:;" role="selectBtn" style="color:#fff;">选择分类</a>&nbsp;&nbsp;&nbsp;<span role="others"><label class="checkbox"><input type="checkbox" name="recommend" />推荐</label><label class="checkbox"><input type="checkbox" name="news" />最新</label><label class="checkbox"><input type="checkbox" name="hot" />热门</span></label><span><input name="keyWord" class="inputText small" type="text"  style="width:250px;" placeholder="搜索内容" /></span>&nbsp;<a class="b_1 miniBtn" href="javascript:;" role="searchBtn">搜索内容</a>&nbsp;<a class="b_1 miniBtn" href="javascript:;" role="allBtn" style="display:none;">全部内容</a></div><div style="padding:10px 0 0 10px;">内容分类：<span role="mainTitle">全部</span></div><div role="container">'+getContentLoading()+'</div></div>'),
		dialog=top.ConfirmDialog(options.title,el,options.win);
	
	var page=1,
		size=8,
		keyWord='',
		categoryId=0,
		getList=function(){
			var data=$.extend(true,options.data,{
					size:size,
					page:page,
					categoryId:categoryId,
					keyWord:keyWord,
					recommend:$('input[name="recommend"]',el).attr('checked')?1:0,
					news:$('input[name="news"]',el).attr('checked')?1:0,
					hot:$('input[name="hot"]',el).attr('checked')?1:0
				});
			el.find('div[role=container]')
			.html(getContentLoading())
			.load(options.url,data
			,function(){
				$.each(dialog.selectedData,function(i,item){
					el.find('div[role=container] li[data-id='+item.id+']').addClass('active');								
				});
				dialog.resizeScroll();
			});
		};
	
	$(':checkbox',el).on('change',function(){
		if($(this).attr('checked')){
			$('a[role="allBtn"]',el).show();
		};
		getList();								   
	});
	
	el.delegate('.selectPage a',click,function(e){
		e.preventDefault();
		e.stopPropagation();
		if(page!=$(this).data('id')){
			page=$(this).data('id');
			getList();
		}
	}).delegate('a[role]',click,function(e){
		switch($(this).attr('role')){
			case 'searchBtn' :
				var keyWordEl=el.find('input[name=keyWord]');
				if(!keyWordEl.val()){
					top.Tips('error','关键字不能为空');
					return;
				};
				if(keyWord!=keyWordEl.val()){
					keyWord=keyWordEl.val();
					page=1;
					getList();
					el.find('a[role=allBtn]').show();
				};
				break;
			
			case 'allBtn':
				var keyWordEl=el.find('input[name=keyWord]');
				keyWord='';
				keyWordEl.val('');
				categoryId=0;
				$('span[role="mainTitle"]',el).html('全部');
				$('span[role="others"] :checkbox',el).attr('checked',false);
				page=1;
				getList();
				$(this).hide();
				break;
			
			case 'selectBtn':
				var sWindow=treeWindow({
					path:$('span[role=mainTitle]',el).html(),
					url:ajaxURL,

					data:{
						action:'getCategory',
						parentId:'-1'
					},
					win:{
						onConfirm:function(){
							categoryId=sWindow.selectedData.id;
							page=1;
							keyWord='';
							$('input[name=keyWord]',el).val('');
							$('span[role=mainTitle]',el).html(sWindow.selectedData.path);
							getList();
							$('a[role=allBtn]',el).show();
						}
					}
				});
				break;
		}
	}).delegate('div.itemSelectList li',click,function(){
		var $this=$(this);
		if(options.multi_selection){
			if($this.hasClass('active')){
				$this.removeClass('active');
				dialog.selectedData=removeArray(dialog.selectedData,{
					id:$this.data('id'),
					name:$this.html()
				});
				   
			}else{
				$this.addClass('active');
				dialog.selectedData.push({
					id:$this.data('id'),
					name:$this.html()
				});
			}
		}else{
			$this.toActive();
			dialog.selectedData=[{
				id:$this.data('id'),
				name:$this.html()
			}];
		}
	});
	
	getList();

	dialog.selectedData=options.selectedData;
	
	return dialog;
};

/**
 *获取外部图片
 */
 
;function getExternalImage(url,callback){
	var nUrl=url;
	$.getJSON(getExternalImage_upload,{picUrl:url},function(data){
		if(data.code == 0){
			if($.isFunction(callback)){
				callback(data.all_path.pic);
			}
		}else{
			top.Alert(data.message);
		}											 
	});
};

/**
 *管理类别
 */

;function setType(opts){
	 var options=$.extend({},{
						  data:'',
						  url:'',
						  title:'类别',
						  nameSpace:'',
						  onEdit:$.noop,
						  onConfirm:$.noop,
						  onClose:$.noop,
						  onAdd:$.noop,
						  onCancel:$.noop
						  },opts),
		title=options.title,
		url=options.url,
		nameSpace=options.nameSpace,
		el=$('<div><div role="itemList" class="list moveList"></div><div class="listAddBtnBox"><a href="javascript:void(0);" role="addItem" class="b_4 bigBtn" style="color:#fff;">添加'+title+'</a></div></div>'),
		typeContainer=$('div.moveList',el),
		addItem=$('a[role="addItem"]',el),
		oldEditListHtml;
	
	/*获得排序*/
	var getSortable=function(){
		var data=[];
		typeContainer.find('ul').each(function(i,item){
			data.push($(item).data('id'));
		});
		return data;
	};
	
	/*保存排序*/
	var sortableList=function(){
		var ids=getSortable();
		rpcJSON(nameSpace+'Sortable',{ids:ids},'','',url);
	};
	
	//创建对话框
	var dialog=top.ConfirmDialog('管理'+title,el,{
			close:function(){
				dialog.find('div[role="itemList"]').sortable( 'destroy');
				var data=[];
				typeContainer.children().each(function(){
					if($(this).data('data').id){
						data.push($(this).data('data'));
					}
				});
				options.onClose(data);
			}
		});
	
	
	$.each(options.data,function(i,item){
		var html='<ul data-id="'+item.id+'"><li class="list1"><span class="arrowTop">'+item.name+'</span></li>';
		if(item.edit){
			html+='<li class="list3"><a href="javascript:void(0);" class="editBtn" title="编辑" role="edit" data-tips="3_t"></a><a href="javascript:void(0);" class="deleteBtn" title="删除" role="del" data-tips="3_t"></a></li>';	
		};
		
		html+='<div class="clear"></div></ul>';
		
		$(html).data('data',item).appendTo(typeContainer);		 
	});
	
	//设置按钮事件
	typeContainer.delegate('a','click',function(){
		var role=$(this).attr('role'),
			ul=$(this).parents('ul:first'),
			data=ul.data('data'),
			id=data.id;
		switch(role){
			
			//编辑序列
			case 'edit':
				$(this).hideTips();
				oldEditListHtml=ul.html();
				var el=$(this).parent();
				var arrowTop=$(this).parent().parent().find('.arrowTop');
				el.html('<a href="javascript:void(0);" class="b_4 miniBtn" role="submit" style="color:#fff">确定</a>&nbsp;<a href="javascript:void(0);" class="b_1 miniBtn"  role="cancel">取消</a></li><div class="clear"></div>');
				arrowTop.html('<input type="text" class="inputText small" value="'+arrowTop.html()+'"  />');
				break;
			
			case 'cancel':
				if(id==0){
					ul.remove();
					addItem.show();
				}else{
					ul.html(oldEditListHtml);
				}
				break;
			break;
			
			case 'submit':
				var el=$(this).parent(),
					arrowTop=ul.find('.arrowTop'),
					name=arrowTop.find('input').val();
				if(name==''){
					top.Alert('名称不能为空');
				}else{
					if(id==0){
						rpcJSON(nameSpace+'Add',{ids:data,name:name},function(backData){
							ul.data('id',backData).data('data',{name:name,id:backData,edit:true});
							el.html('<a href="javascript:void(0);" class="editBtn" title="编辑" data-tips="3_t" role="edit"></a><a href="javascript:void(0);" class="deleteBtn" title="删除" data-tips="3_t" role="del"></a>');
							arrowTop.html(arrowTop.find('input').val());
							addItem.show();
							dialog.resizeScroll();
						},'',url);
					}else{
						rpcJSON(nameSpace+'Edit',{id:id,name:name},function(backData){
							ul.data('data',{name:name,id:id,edit:true});
							el.html('<a href="javascript:void(0);" class="editBtn" title="编辑" data-tips="3_t" role="edit"></a><a href="javascript:void(0);" class="deleteBtn" title="删除" data-tips="3_t" role="del"></a>');
							arrowTop.html(arrowTop.find('input').val());
							
						},'',url);
					}
				}
			break;
			
			case 'del':
				top.Confirm('确定要删除该'+title+'吗？',function(){
					 ul.remove();
					 rpcJSON(nameSpace+'Del',{id:id},'','',url);
					 dialog.resizeScroll();
				});
			break;
		}
		
	});
	dialog.find('div[role="itemList"]').sortable({
				revert: false,
				axis:'y',
				update:sortableList	
			});
	
	//添加
	el.delegate('a[role="addItem"]',click,function(e){
		var html='<li class="list1"><span class="arrowTop"><input type="text" class="inputText small"  /></span></li>';
			html+='<li class="list3"><a href="javascript:void(0);" class="b_4 miniBtn" role="submit" style="color:#fff">确定</a>&nbsp;<a href="javascript:void(0);"  class="b_1 miniBtn"  role="cancel">取消</a></li><div class="clear"></div>';
			var newItem=$('<ul data-id="0"></ul>').data('data',{id:0,name:'',edit:true});
			$(newItem).html(html).appendTo(typeContainer);
			$(this).hide();
			typeContainer.find('div.empty').remove();
													   
	});
};

/**
 *设置链接列表
 */

;function setUrlList(container,items,rpcurl,title,hideContent){
	
	if(!container)return;
	
	container=$(container);
	
	title=title||'导航';
	
	var html='<div class="list"><ul class="title"><li class="list1"><h3>名称</h3></li><li class="list2"><h3>链接</h3></li><li class="list3"><h3>操作</h3></li></ul><div class="clearfix"></div></div><div class="list moveList">',
	    hideContentBtn=hideContent?'<a href="javascript:void(0);" class="plusBtn" data-tips="3_t" title="设置隐藏区域" role="plus"></a>':'',
		toolsHtml='<a href="javascript:void(0);" class="editBtn" title="编辑" data-tips="3_t" role="edit"></a><a href="javascript:void(0);" class="deleteBtn" title="删除" data-tips="3_t" role="del"></a>'+hideContentBtn+'<a href="javascript:void(0);" class="enterBtn" data-tips="3_t" title="打开链接" role="enter"></a>';
			
  $.each(items,function(i,item){
	  var id=item.id?item.id:'';
	  
	  html+='<ul data-id="'+id+'">'+
			  '<li class="list1"><span class="arrowTop">'+item.name+'</span></li>'+
			  '<li class="list2">'+item.url+'</li>'+
			  '<li class="list3">'+toolsHtml+
			  '</li>'+
			  '<div class="clear"></div>'+
		  '</ul>';
	  
  });
  
  html+='</div><div class="listAddBtnBox"><a href="javascript:void(0);" role="addItem" class="b_4 bigBtn">添加'+title+'</a></div>';
  
  container.html(html);
  
  if(hideContent){
	  $.each(items,function(i,item){
			if(items[i]['hideContent']){
				$('a.plusBtn:eq('+i+')',container).addClass('active');
				$('div.moveList ul:eq('+i+')',container).data('hidecontent',items[i]['hideContent']);
				
			}
	  });
  };
  
  var itemList=$('div.moveList',container),
  	  addItemBtn=$('a[role=addItem]',container);
  
  /*获得排序*/
  ;function getSortable(){
	  var data=[];
	  itemList.find('ul').each(function(i,item){
		  data.push($(item).data('id'));
	  });
	  return data;
  };
  
  /*保存排序*/
  ;function sortableList(){
	  var ids=getSortable();
	  if(rpcurl){	
		  rpcJSON('sortable',{ids:ids},'','',rpcurl);
	  }
  };
  
 
  
  /*初始化排序*/
  var oldEditListHtml='';
  itemList.delegate('ul',{
	  mouseenter:function(){
		  $(this).find('.arrowTop').addClass('arrowTopSelect');
	  },
	  mouseleave:function(){
		  $(this).find('.arrowTop').removeClass('arrowTopSelect');
	  }
  }).delegate('a[role]',click,function(e){
	  switch($(this).attr('role')){
		  //编辑序列
		  case 'edit':
			  $(this).hideTips();
			  oldEditListHtml=$(this).parent().parent().html();
			  var el=$(this).parent();
			  var arrowTop=$(this).parent().parent().find('.arrowTop');
			  var list2=$(this).parent().parent().find('.list2');
			  el.html('<a href="javascript:void(0);" class="b_4 miniBtn" role="submit">确定</a>&nbsp;<a href="javascript:void(0);" class="b_1 miniBtn"  role="cancel">取消</a></li><div class="clear"></div>');
			  arrowTop.html('<input type="text" class="inputText small" value="'+arrowTop.html()+'"  />');
			  list2.html('<input type="text" class="inputText small" name="href" value="'+list2.html()+'" style="width:90%" />&nbsp;<a  href="javascript:void(0);"  role="selectPageUrl"><i class="viewUrlIco" title="选择本站页面" data-tips="3_t"></i></a>');
			  break;
		  case 'submit':
			  var ul=$(this).parent().parent();
			  var id=ul.data('id');
			  var el=$(this).parent();
			  var arrowTop=ul.find('.arrowTop');
			  var list2=ul.find('.list2');
			  var name=arrowTop.find('input').val();
			  var href=list2.find('input').val();
			  if(name==''){
				  Alert(title+'名称不能为空');
			  }else if(!href||href=="http://"){
				  Alert('请输入正确的链接');
				  list2.find('input').val('http://');
			  }else{
				href=href.replace(/"/g,'%22');
				 list2.find('input').val(href);
				  if(id==0){
					  var data=getSortable(),
						  fn=function(backData){
							  if(backData){
								  ul.data('id',backData.id);
							  };
							  el.html(toolsHtml);
							  arrowTop.html(arrowTop.find('input').val());
							  list2.html(list2.find('input').val());
							  addItemBtn.show();
						  };
					  if(rpcurl){	
					  	  data={ids:data,name:name,href:href};
						  if(hideContent){
							  data['hideContent']=ul.data('hideContent');
						  }; 	
						  rpcJSON('add',data,fn,'',rpcurl);
					  }else{
						  fn(0);
					  }
				  }else{
					  var fn=function(backData){
							  el.html(toolsHtml);
							  arrowTop.html(arrowTop.find('input').val());
							  list2.html(list2.find('input').val());
						  };
					  if(rpcurl){
						   data={id:id,name:name,href:href};
						  if(hideContent){
							  data['hideContent']=ul.data('hideContent');
						  };
						  rpcJSON('edit',data,fn,'',rpcurl);
					  }else{
						  fn(0);
					  }
				  }
			  }
			  break;
			  
		  case 'cancel':
			  var id=$(this).parent().parent().data('id');
			  if(id=='0'){
				  $(this).parent().parent().remove();
				 addItemBtn.show();
			  }else{
				  $(this).parent().parent().html(oldEditListHtml);
			  }
			  break;
			  
		  case 'del':
			  var ul=$(this).parent().parent(),
				  id=ul.data('id'),
				  fn=function(backData){
					  ul.hideRemove();
				  };
			  Confirm('确定要删除这个'+title+'吗？',function(){
				  if(rpcurl){	
					  rpcJSON('del',{id:id},fn,'',rpcurl);
				  }else{
					  fn(0);
				  }						   
			  });	
			  break;
			  
		  case 'enter':
			  var ul=$(this).parent().parent();
			  var href=ul.find('li.list2').html();
			  window.open(href);	
			  e.preventDefault();	
			  break;
			  
		   case 'selectPageUrl':
				selectPageUrl($(this).parents('li:first').find('input[name="href"]'),ajaxURL);
			break;
			
			case 'plus':
				var $this=$(this),
					ul=$(this).parents('ul:first'),
					el=$('<div style="width:100%;height:100%"><iframe src="/?c=custom&a=setText" width="100%" height="100%" frameborder="0" scrolling="no"></iframe></div>');
				top.$HASH['setText']={
					text:ul.data('hidecontent')?ul.data('hidecontent'):'',
					canEmpty:true
				};
				top.ConfirmDialog('设置隐藏区域内容,仅电脑上有效',el,{
					scroll:false,			  
					 onBeforeClose:function(fn,type){
						if(type=='confirm'&&top.$HASH['setText']['check']){
							top.$HASH['setText']['check'](fn);
						}else{
							fn();
						}
					},
					
					onConfirm:function(){
						var content=top.$HASH['setText']['backData']['text'];
						 ul.data('hidecontent',content);
						 if(content){
							 $this.addClass('active');
						 }else{
							$this.removeClass('active'); 
						 };
						 if(rpcurl){	
							data={id:ul.data('id'),hideContent:content};
							rpcJSON('setHideContent',data,'','',rpcurl);
						}
					}
				});	
			break;
			  
		  }
	  }).sortable({
		  revert: false,
		  axis:'y',
		  update:sortableList	
	  }); 
  
  
	  /*添加序列*/
	 addItemBtn.click(function(){
		  var html='<li class="list1"><span class="arrowTop"><input type="text" class="inputText small"  /></span></li>';
		  html+='<li class="list2"><input type="text" class="inputText small"  name="href" value="http://"  style="width:90%"  />&nbsp;<a  href="javascript:void(0);"  role="selectPageUrl"><i class="viewUrlIco" title="选择本站页面" data-tips="3_t"></i></a></li>';
		  html+='<li class="list3"><a href="javascript:void(0);" class="b_4 miniBtn" role="submit">确定</a>&nbsp;<a href="javascript:void(0);"  class="b_1 miniBtn"  role="cancel">取消</a></li><div class="clear"></div>';
		  var newItem=$('<ul data-id="0"></ul>');
		  $(newItem).html(html).appendTo(itemList);
		  $(this).hide();
		  itemList.find('div.empty').remove();
	  });
  
   /*获得数据*/
	 this.getItems=function(){
		  $('a[role="cancel"]',itemList).trigger(click);
		  
		  var returnItems=[];
		  $('ul',itemList).each(function(){
				  var data={
						  name:$(this).find('li.list1').text(),
						  url:$(this).find('li.list2').text()
					  };
				  if(hideContent){
					  data['hideContent']=$(this).data('hidecontent');
				  };
				  if($(this).data('id')){
					  data.id=$(this).data('id');
				  };
				  returnItems.push(data);
		  });
		  return returnItems;
	  };
	
	return this;
};

/**
 *选择本站页面
 */

;function selectPageUrl(target,url,callback){
	
	
	var el=$('<div><div role="tab" style="border-bottom:1px solid #ccc;"><ul class="tab_1 clearfix"><li role="getPagesJSON"><span>基本网页</span></li><li role="getAppPagesJSON"><span>应用网页</span></li></ul></div><div role="container" class="itemSelectList p10"></div></div>'),
		container=$('div[role="container"]',el),
		role='';
	
	var dialog=top.ConfirmDialog('选择本站页面',el,{
		onBeforeClose:function(fn,type){
			if(type=='confirm'){
				if(!$('li.active',container).length){
					top.Alert('还没有选择页面');
				}else{
					fn();
				};
			}else{
				fn();
			};
		},
		onConfirm:function(){
			var selected=$('li.active',container),
				id=selected.data('id'),
				pageurl=role=='getPagesJSON'?'/?c=default&pageId='+id:'/?c=default&a=view&ac='+id;
			if(target){
				target.val(pageurl);
			};
			if($.isFunction(callback)){
				callback({
						 	name:selected.text(),
							href:pageurl
						 });			
			};
		}
	});
	
	$('div[role="tab"] li',el).litabs({
		callBack:function(btn){
			container.html(getContentLoading());
			role=btn.attr('role');
			$.getJSON(url,{action:role},function(backData){
				if(backData.items&&	backData.items.length){
					var html='<ul>';
					$.each(backData.items,function(i,item){
						html+='<li data-id="'+item.id+'">'+item.name+'</li>';						   
					});
					html+='</ul>';
					container.html(html);
					dialog.resizeScroll();
				}
			});
		}
	});
	
	container.delegate('div.itemSelectList li',click,function(){
		$(this).toActive();
	});
};